const {Tool, API, Storage, Event} = global.$util
const app = getApp()
// import MyOrderFactory from '../../pages/my/orderDetail/myOrder'

Component({
    properties: {
        num: Number,
        condition: String,
    },
    data: {
        showMoreBtn: false,
        num: 0,
        params: {},
        totalPage: '', // 页面总页数
        currentPage: 1, // 当前的页数
        pageSize: 10, // 每次加载请求的条数 默认10
        list: [],
        isEmpty: false,//是否暂无数据
        isCancel: false,//是否取消订单
        isDelete: false, //是否删除订单
        orderId: '',
        orderNum: '',
        key: 0,
        time: "",
        isAjax: false,
        showAdviserAlert:false,
		exitFreeGroup:false,
		hasRequest:false
    },
    methods: {
        //获取列表数据
        getList(e) {
            this.setData({
                isEmpty: false,
                isMore: false,
                list: []
            })
			this.data.num = this.properties.num
            this.data.currentPage = 1
            this.data.key = 0
            this.getData(this.properties.num)
        },
        //获取数据
        getData(index) {
			let list = this.data.list
            let params = {
                pageSize: this.data.pageSize,
                pageNum: this.data.currentPage,
                status: index || '',
                keywords: this.properties.condition || '', // 关键字
            }
            if (index == 4) {
                params.commentStatus = true
            }
            API.getOrderList(params).then((res) => {
                let datas = res.data || {}
                let key = this.data.key
                let list = datas.data || []
                let isEmpty = false
                if (!datas.total) {
                    isEmpty = true
                }
                let isMore = (datas.currentPage < datas.totalPage || datas.isMore) ? true : false
                
                this.setData({
                    list: this.data.list.concat(list),
                    key: key,
                    isEmpty: isEmpty,
                    isMore: isMore
                }, ()=> {
                    this.data.loadMoreView = this.selectComponent(".loadMoreView")
                    this.data.loadMoreView && this.data.loadMoreView.loadMoreComplete()
                    this.triggerEvent('isAjax');
                });

            }).catch((res) => {
                this.triggerEvent('isAjax');
                console.log(res)
            })
        },
        // 上拉加载更多
        onReachBottom() {
            this.data.loadMoreView.loadMore()
        },
        _loadMoreListener(){
            this.data.currentPage++
            this.getData(this.data.num);
        },
        //跳到订单详情
        toOrderDetail(e) {
            Tool.navigateTo('/pages/my/orderDetail/orderDetail?orderId=' + e.currentTarget.dataset.id + '&status=' + e.currentTarget.dataset.status + '&num=' + this.data.num)
        },
        
        //删除订单
        deleteItem(index) {
            this.data.spliceIndex = index
            this.data.deleteItemId = this.data.list[index].showOrderNo
            this.setData({
                isDelete: true,
            });
        },
        deleteOrder() {
            MyOrderFactory.deleteOrder(
                this.data.deleteItemId,
                ()=>{
                    this.setData({
                        isDelete: false,
                    });
                    this.changeOrderStatus(99)
                },
                ()=>{
                    this.setData({
                        isDelete: false,
                    })
                }
            )
        },
        cancelOrder() {
            this.setData({
                isCancel: false,
                list: this.data.list,
                key: 0
            });
            this.changeOrderStatus(5)
        },
        
        cancelItem(index) {
            // let index = e.currentTarget.dataset.index
            this.data.spliceIndex = index
            this.setData({
                isDelete: false,
                platformOrderNo: this.data.list[index].baseInfo.platformOrderNo,
                clickedType: 1,
            }, ()=> {
                this.selectComponent("#cancelOrder").getAllProductOrder()
            });
        },
        
        orderBtn(item){
			return MyOrderFactory.getOrderBtn(item)
        },
    }
})
